<?php
global $_W, $_GPC;
$uniacid = $_W['uniacid'];
$config = $this->module['config'];
$uid = $_GPC['uid'];
$isbind = $_GPC['isBanding'];//1登录  2绑定
$op = empty($_GPC['op']) ? 'wxlogin' : $_GPC['op'];
$oauth2 = new Wxapi($config['h5_appid'], $config['h5_appsecret']);
if ($op == 'wxlogin') {
    $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] . '&op=getcode';
    $re = $oauth2->getCode($url, 'snsapi_userinfo');
} elseif ($op == 'getcode') {
    $code = $_GPC['code'];
    if (!empty($code)) {
        if ($isbind == 1) {//登录
            header("location: " . $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'] .  "/addons/goouc_class/vue/index.html?i=".$uniacid."#/phoneLogin?code=" . $code);
            exit;
        } elseif ($isbind == 2) {//绑定
            header("location: " . $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'] .  "/addons/goouc_class/vue/index.html?i=".$uniacid."#/binding-wx?code=" . $code);
            exit;
        }
    }
} elseif ($op == 'getuserinfo') {
    $code = $_GPC['code'];
    if (!empty($code)) {
        $resultArray = $oauth2->getUsertoken($code);
        //通过access_token 获取用户详细信息  绑定微信开放平台后会出现 unionid的值
        $userinfo = $oauth2->getUserinfo($resultArray['access_token'], $resultArray['openid']);
        //绑定还是登录:没有uid是登录（检测表里是否有和状态），
        $has = pdo_get($this->t_user, array('weid' => $_W['uniacid'], 'h5_openid' => $userinfo['openid']), array('id', 'status'));
        if ($isbind == 1) {
            if (!$has['id']) {//没绑定
                return $this->getresult(0, '请先用手机号登录', ['code' => 1003]);
            } elseif ($has['status'] == 0) {
                return $this->getresult(0, '用户被禁用，请联系管理员', ['code' => 1003, 'uid' => $has['id']]);
            } else {
                //给用户生成token验证值
                $token = $this->createtoken();
                $dat = [
                    'weid' => $_W['uniacid'],
                    'uid' => $has['id'],
                    'token' => $token,
                    'createtime' => time()
                ];
                //插入user_token数据表
                $has_token = pdo_get($this->t_user_token, ['weid' => $_W['uniacid'], 'uid' => $checkM['id']]);
                if ($has_token) {
                    $ress = pdo_update($this->t_user_token, $dat, array('id' => $checkM['id']));
                } else {
                    //插入user_token数据表
                    $ress = pdo_insert($this->t_user_token, $dat);
                }
                if (!$ress) {
                    return $this->getresult(0, '生成token失败了,请重新登录', ['code' => 4001, 'uid' => $has['id']]);
                }
                return $this->getresult(0, '登陆成功', ['code' => 200, 'uid' => $has['id'], 'token' => $token]);
            }
        } else {
            //检测是否绑定
            if (!empty($has)) {
                if ($has['id'] != $uid) {
                    return $this->getresult(0, '该微信已经绑定了别的账户', ['code' => 1003]);
                }
            } else {
                /***校验token开始***/
                if($uid){
                    $token = $_GPC['token'];
                    if (empty($token)) {
                        return $this->getresult(1, '请重新登录', ['code' => 4001, 'msg' => '请重新登录']);
                    }
                    $token_result = $this->checktoken($uid, $token);
                    if ($token_result == 1002) {
                        return $this->getresult(1, '请重新登录', ['code' => 4002, 'msg' => '请重新登录']);
                    } elseif ($token_result == 1003) {
                        return $this->getresult(1, '请重新登录', ['code' => 4003, 'msg' => '请重新登录']);
                    }elseif ($token_result == 1001) {
                        //  更新token
                        pdo_update($this->t_user_token,array('createtime'=>time()),array('uid' => $uid, 'token' => $token, 'weid' => $_W['uniacid']));
                    }
                }
                /***校验token结束***/
                //开始绑定
                $bindData = [
                    'h5_openid' => $userinfo['openid'],
                    'nickname' => $userinfo['nickname'],
                    'headimg' => $userinfo['headimgurl'],
                ];
                $res = pdo_update($this->t_user, $bindData, array('id' => $uid));
                if ($res) {
                    return $this->getresult(0, '绑定成功', ['code' => 200]);
                }
            }
        }
    } else {
        return $this->getresult(0, '未获取到code', ['code' => 1003]);
    }

}